#include "Dodgeball.h"


CDodgeball::CDodgeball()
{
}


CDodgeball::~CDodgeball()
{
}

void CDodgeball::Update(float elapsedTime)
{
	if (explosion == false)
	{

		CBall::Update(elapsedTime);

		if (this->m_bIsHeld == false && this->m_bAerial == false && spawn == false)
		{
			bombtimer -= elapsedTime;
		}
		if (bombtimer <= 0.0f && explosion == false)
		{
			Explosion();
		}
	}
	else
		ToggleActive(false);

}

void CDodgeball::Explosion()
{
	explosion = true;
	this->m_ptPosition.x -= 16;
	this->m_ptPosition.y -= 16;
	this->m_szSize.height += 16;
	this->m_szSize.width += 16;
}

void CDodgeball::Render()
{
	if (explosion == false)
	{
		CBall::Render();
	}
	else
	{
		// explosion effect
	}
}

void CDodgeball::HandleCollision(const Entity* pOther)
{
	if (pOther->GetType() == ENT_PLAYER)
	{
		if (dynamic_cast<const CAgent*>(pOther)->GetIsExhausted() == false && m_bAerial == false)
		{
			m_bIsHeld = true;
			this->spawn = false;
			this->SetTimer(8.0f);
			ToggleActive(false);
		}

		else if (pOther->CheckActive() == true && this->GetOwner() != reinterpret_cast<const CPlayer*>(pOther) && explosion == false)
			Explosion();
	}

	Entity* wall = const_cast<Entity*>(pOther);

	if (pOther->GetType() == ENT_WALL && dynamic_cast<CWall*>(wall)->GetImpenetrable() == true)
	{

		// Is the agent in between the wall's top and bottome?
		if (m_ptPosition.y + 1 <= pOther->GetPosition().y + pOther->GetSize().height - 1 &&
			m_ptPosition.y + m_szSize.height - 1 >= pOther->GetPosition().y + 1)
		{
			// is the agent to the left of the wall?
			if (m_ptPosition.x + m_szSize.width > pOther->GetPosition().x && m_ptPosition.x + m_szSize.width < pOther->GetPosition().x + pOther->GetSize().width)
				m_ptPosition.x -= (m_ptPosition.x + m_szSize.width) - pOther->GetPosition().x;
			// is the agent to the right of the wall?
			else if (m_ptPosition.x < pOther->GetPosition().x + pOther->GetSize().width && m_ptPosition.x > pOther->GetPosition().x)
				m_ptPosition.x += (pOther->GetPosition().x + pOther->GetSize().width) - m_ptPosition.x;
		}
		else
		{
			// is the agent above the wall?
			if (m_ptPosition.y + m_szSize.height > pOther->GetPosition().y && m_ptPosition.y + m_szSize.height < pOther->GetPosition().y + pOther->GetSize().height)
				m_ptPosition.y -= (m_ptPosition.y + m_szSize.height) - pOther->GetPosition().y;
			// is the agent below the wall?
			else if (m_ptPosition.y < pOther->GetPosition().y + pOther->GetSize().height && m_ptPosition.y > pOther->GetPosition().y)
				m_ptPosition.y += (pOther->GetPosition().y + pOther->GetSize().height) - m_ptPosition.y;
		}
	}
}
